package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.Menu;
import com.serkol.ams.mapper.MenuMapper;
import com.serkol.ams.service.MenuService;
import com.serkol.ams.service.SysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【menu】的数据库操作Service实现
 * @createDate 2022-10-07 17:12:39
 */

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
        implements MenuService {
    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Cacheable(cacheNames = "menu", key = "'fetchMenuList:'+#roleId")
    @Override
    public List<Map<String, Object>> fetchMenuList(Long roleId) {
        List<Long> myRolePermissionsIds = sysRolePermissionService.getMyRolePermissionsIds();
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().isNull(Menu::getParentId).eq(Menu::isMenuShow, true);
        queryWrapper.lambda().between(Menu::getMenuType, 0, 1);

        List<Map<String, Object>> list = this.baseMapper.fetchMenuList(queryWrapper);
        List<Map<String, Object>> listMap = new ArrayList<>();

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();


            Object id = next.get("id");
            List<Map<String, Object>> list1 = fetchMenuParentList(id, myRolePermissionsIds);
            if (list1.size() > 0) {
                //  next.put("hideChildrenInMenu",true);
                //  next.put("hideInMenu",false);
            } else {
                // next.put("hideChildrenInMenu",false);
            }
            next.put("routes", list1);
            listMap.add(next);
        }

        return listMap;
    }

    public List<Map<String, Object>> fetchMenuParentList(Object parentId, List<Long> myRolePermissionsIds) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Menu::getParentId, parentId).eq(Menu::isMenuShow, true);
        List<Map<String, Object>> list = this.baseMapper.fetchMenuList(queryWrapper);
        List<Map<String, Object>> listMap = new ArrayList<>();

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();


            Object id = next.get("id");

            List<Map<String, Object>> list1 = fetchMenuParentList(id, myRolePermissionsIds);
            if (list1.size() > 0) {
                //  next.put("hideChildrenInMenu",true);
                //  next.put("hideInMenu",false);
            } else {
                // next.put("hideChildrenInMenu",false);
            }
            next.put("routes", list1);
            listMap.add(next);
        }
        return listMap;
    }

    @Override
    public List<Map<String, Object>> fetchMenuTreeList() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().isNull(Menu::getParentId);

        List<Map<String, Object>> list = this.baseMapper.fetchMenuTreeList(queryWrapper);
        List<Map<String, Object>> listMap = new ArrayList<>();

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();
            Object id = next.get("id");
            List<Map<String, Object>> list1 = fetchMenuTreeParentList(id);
            if (list1.size() > 0) {
                next.put("hideChildrenInMenu", true);
                next.put("hideInMenu", false);
                next.put("children", list1);
            } else {
                next.put("hideChildrenInMenu", false);
            }

            listMap.add(next);
        }
        return listMap;
    }

    public List<Map<String, Object>> fetchMenuTreeParentList(Object parentId) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Menu::getParentId, parentId);

        List<Map<String, Object>> list = this.baseMapper.fetchMenuTreeList(queryWrapper);
        List<Map<String, Object>> listMap = new ArrayList<>();

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();

            Object id = next.get("id");
            List<Map<String, Object>> list1 = fetchMenuTreeParentList(id);
            if (list1.size() > 0) {
                next.put("hideChildrenInMenu", true);
                next.put("hideInMenu", false);
                next.put("children", list1);
            } else {
                next.put("hideChildrenInMenu", false);
            }

            listMap.add(next);
        }
        return listMap;
    }

    @Override
    public List<Map<String, Object>> listSelect() {
        QueryWrapper<Menu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().isNull(Menu::getParentId);
        queryWrapper1.select("NAME  as label,ID as value");
        List<Map<String, Object>> list = this.baseMapper.selectMaps(queryWrapper1);

        return list;
    }

    @Override
    public boolean byDelete(long id) {
        if (id > 0) {
            return this.removeById(id);

        }
        return false;
    }

}




